local armorDefs = {
  TORPEDO_RESISTANT = {
    "c_monitor",
    --"c_factory",	
  },
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function calculateDamageToArmor (damage, armorPenetration, armorThickness)
  armorPenetration = math.max(0, armorPenetration)
  local penetrationFactor = math.min((100 + armorPenetration) / (100 + armorThickness), 1) 
  return (damage * penetrationFactor)
end

do
  local UnitDefs = DEFS.unitDefs
  local WeaponDefs = DEFS.weaponDefs
  
  local unitToArmorDef = {}
  for categoryName, categoryTable in pairs(armorDefs) do
    for _, unitName in ipairs(categoryTable) do
	  if (UnitDefs[unitName]) then
      unitToArmorDef[unitName] = categoryName:lower()
	  end	
    end
  end
	
  local processedArmorDefs = {}
  local oldDefsToNewDefs = {}  
  for unitName, ud in pairs(UnitDefs) do
    local category = unitToArmorDef[unitName] or "other"
    local armor = ud.customparams.armorthickness or "0"
    local fullCategory = category .. "_armor" .. armor
    
    if (not processedArmorDefs[fullCategory]) then
      oldDefsToNewDefs[category] = oldDefsToNewDefs[category] or {}
      oldDefsToNewDefs[category][armor] = fullCategory

      processedArmorDefs[fullCategory] = {}
    end
    processedArmorDefs[fullCategory][unitName] = 1	
  end
  
  armorDefs = processedArmorDefs
  
  --VFS.Include("luarules/utils/debug.lua")
  --printTable ("armorDefs", armorDefs)
  
  local function fixDamages (damageArray, armorPenetration)
    if (not damageArray) then return nil; end
    
	  for category, v in pairs(damageArray) do
      if (type(v) == "string") then
        damageArray[category] = tonumber(v)
      end
    end 

    damageArray.other = damageArray.other or damageArray.default or 0
    local newDamageArray = { default = damageArray.default or damageArray.other }	
  
    for category, armorTable in pairs(oldDefsToNewDefs) do
      local damageAmount = damageArray[category] or damageArray.other	
      for armor, newCategory in pairs(armorTable) do
	      local newDamage = calculateDamageToArmor (damageAmount, armorPenetration, armor)
		    if (newDamage ~= newDamageArray.default) then
          newDamageArray[newCategory] = newDamage
	      end  
	    end
    end
	
	  return newDamageArray
  end
  
  -- set weapon damages to the new categories
  for weaponName, wd in pairs(WeaponDefs) do
    local armorPenetration = wd.customparams.armorpenetration or 0
    if (wd.impactOnly) then
	    wd.damage = fixDamages (wd.damage, armorPenetration)
	  else  
	    wd.damage = fixDamages (wd.damage, armorPenetration)  --(wd.damage, 0) --temporary
  	  if (wd.customparams.impactdamage) then
        wd.customparams.impactdamage = fixDamages (wd.customparams.impactdamage, armorPenetration)
	    end
    end	  
	
    --printTable ("WeaponDefs["..weaponName.."].damages", wd.damage)
    --printTable ("WeaponDefs["..weaponName.."].impactDamages", wd.customparams.impactdamage)
    --printTable ("WeaponDefs["..weaponName.."].params", wd.customparams)	
  end  
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

return armorDefs

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------